/**
 * 上网行为管理api
 */

import request from '@/utils/request';

//  文件下载接口
export function formidableUtil(res, fileName) {
    let blob = new Blob([res], { type: "application/vnd.ms-excel" })
    fileName = fileName || '下载.png';
    if ('download' in document.createElement('a')) {
        // 非IE下载
        const elink = document.createElement('a')
        elink.download = fileName
        elink.style.display = 'none'
        elink.href = URL.createObjectURL(blob)
        document.body.appendChild(elink)
        elink.click()
        URL.revokeObjectURL(elink.href) // 释放URL 对象
        document.body.removeChild(elink)
    } else {
        // IE10+下载
        navigator.msSaveBlob(blob, fileName)
    }
}

//上网行为日志-分页查询
function surfData(data) {
    return request({
        url: 'drug/surf/data',
        method: 'get',
        params: data
    })
}

//上网流量统计
function selectTrafficStatistics(data) {
    return request({
        url: 'drug/surf/selectTrafficStatistics',
        method: 'post',
        data
    })
}

//设备管理
function selectDevice(data) {
    return request({
        url: 'drug/surf/selectDevice',
        method: 'post',
        data
    })
}

//导出上网流量统计
function surfExportData(data) {
    return request({
        url: 'drug/surf/exportData',
        method: 'post',
        data,
        responseType: 'blob'
    }).then(res => {
        formidableUtil(res, '上网流量统计.xls')
    });
}

//导出上网行为统计
function downloadFailedUsingJson(data) {
    return request({
        url: 'drug/surf/downloadFailedUsingJson',
        method: 'post',
        data,
        responseType: 'blob'
    }).then(res => {
        formidableUtil(res, '上网流量统计.xls')
    });
}

export default {
    surfData,
    selectTrafficStatistics,
    selectDevice,
    surfExportData,
    downloadFailedUsingJson
}